package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    //分页查询
    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("查询请求参数： {}", empQueryParam);
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }
    //保存员工
    @PostMapping
    public Result sava(@RequestBody Emp emp) {
        log.info("保存员工，员工信息：{}", emp);
        empService.save(emp);
        return Result.success();
    }
    //查询所有
    @GetMapping("/list")
    public Result findAll(){
        return Result.success(empService.list());
    }

    //删除和批量删除
    @DeleteMapping
    public  Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工，员工id：{}", ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    //修改员工信息,数据回显
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询员工信息，员工id：{}", id);
        return Result.success(empService.getById(id));
    }

    //员工信息修改
    @PutMapping
    public Result update(@RequestBody Emp emp){
            empService.update(emp);
            return Result.success();
    }




}
